package com.client;

import com.model.*;

import java.io.*;
import java.util.List;

/**
 * 考试管理子模块
 */
public class ExamSubSystemView {
    private ClientInitClose clientInitClose;
    private StudentMessage studentMessage;
    private int score = 0;

    public ExamSubSystemView() {
    }

    public ExamSubSystemView(ClientInitClose clientInitClose) {
        this.clientInitClose = clientInitClose;
    }

    public void examSubSytemMenu(UserMessage userMessage) throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("------------------------------");
            System.out.println("[1] 开始考试         [2] 导出成绩");
            System.out.println("[3] 查询成绩         [0] 退出");
            System.out.println("------------------------------");
            System.out.println("请选择您要操作的模块：");
            int option = ClientScanner.getScanner().nextInt();
            User temp_user = userMessage.getUser();
            switch (option){
                case 1:
                    //System.out.println("开始考试。");
                    //userMessage.setMessage("startExam");
                    // 发送给服务器开始考试
                    clientInitClose.getObjectOutputStream().writeObject(new UserMessage("startExam",temp_user));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器内容
                    studentMessage = (StudentMessage)clientInitClose.getObjectInputStream().readObject();
                    if (studentMessage.getMessage().equals("successStartExam")){
                        List<Exam> exams = studentMessage.getStudent().getExams();
                        System.out.println("考试开始！共有 " + exams.size() + " 道题，每题1分。");
                        int num = 0;
                        // 开始答题
                        for (Exam exam:exams){
                            System.out.println(exam.getQuestion() + "   " + exam.getAnswers());
                            String ans = ClientScanner.getScanner().next();
                            if (ans.equals(exam.getRightAnswer()))
                                num++;
                        }
                        this.score = num * 1;
                        System.out.println("考试结束！共答对 " + num + " 道，得分为：" + this.score);
                        // 把考试成绩传到服务器
                        clientInitClose.getObjectOutputStream().writeObject(this.score);
                        clientInitClose.getObjectOutputStream().flush();
                        System.out.println("上传成绩成功！");
                    }else
                        System.out.println("考试失败！");
                    break;
                case 2:
                    //System.out.println("导出成绩。");
                    //userMessage.setMessage("ExportScore");
                    // 客户端给服务器发送导出成绩
                    clientInitClose.getObjectOutputStream().writeObject(new UserMessage("ExportScore",temp_user));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务端
                    studentMessage = (StudentMessage) clientInitClose.getObjectInputStream().readObject();
                    if (studentMessage.getMessage().equals("successExportScore")){
                        // 导出成绩
                        File file = new File("./ExamSystemClient/src/com/ExportScore.txt");
                        FileOutputStream fileOutputStream = null;
                        ObjectOutputStream objectOutputStream = null;
                        try {
                            // 写入客户端本地
                            fileOutputStream = new FileOutputStream(file);
                            objectOutputStream = new ObjectOutputStream(fileOutputStream);
                            objectOutputStream.writeUTF(studentMessage.getStudent().toString());
                            objectOutputStream.flush();

                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }finally {
                            if (objectOutputStream != null){
                                try {
                                    objectOutputStream.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }if (fileOutputStream != null){
                                try {
                                    fileOutputStream.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                            System.out.println("导出学员成绩成功！");
                        }
                    }else
                        System.out.println("导出学员成绩失败！");
                    break;
                case 3:
                    //System.out.println("查询成绩。");
                    //userMessage.setMessage("queryScore");
                    // 客户端发送查询成绩
                    clientInitClose.getObjectOutputStream().writeObject(new UserMessage("queryScore",temp_user));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器返回消息
                    studentMessage = (StudentMessage) clientInitClose.getObjectInputStream().readObject();
                    //System.out.println(studentMessage);
                    if (studentMessage.getMessage().equals("successQueryScore")){
                        Student receiveStudent = studentMessage.getStudent();
                        System.out.println("id为 " + receiveStudent.getId() + "，姓名为 " + receiveStudent.getUserName() + " 的成绩是：" + receiveStudent.getScore());
                    }else
                        System.out.println("查询学员成绩失败！");
                    break;
                case 0:
                    System.out.println("正在退出考试系统。。。。。。");
                    //userMessage.setMessage("quit");
                    clientInitClose.getObjectOutputStream().writeObject(new UserMessage("quit",temp_user));
                    clientInitClose.getObjectOutputStream().flush();
                    return;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }
}
